package com.chpro.spittr.alerts;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.MessageCreator;

import com.chpro.spittr.domain.Spittle;
/**
 * 消息发送类
 * @author Administrator
 *
 */
public class AlertServiceImpl implements AlertService {

	/**
	 * jmsTemplate实现的接口
	 */
	private JmsOperations jmsOperations;

	public AlertServiceImpl(JmsOperations jmsOperations) {
	    this.jmsOperations = jmsOperations;
	}
	
	/**
	 * 发送一个消息
	 */
	public void sendSpittleAlert(final Spittle spittle) {
		jmsOperations.send("spitter.queue", new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage(spittle);
			}
		});
	}

	/**
	 * 获取一个消息
	 * @return
	 */
	public Spittle receiveSpittleAlert()throws Exception{
		ObjectMessage objectMessage = (ObjectMessage)jmsOperations.receive("spitter.queue");
		return (Spittle)objectMessage.getObject();
	}
}
